package com.ycode.bang.anbang.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.model.RegisiterModel;
import com.ycode.bang.anbang.utils.CommonTools;
import com.ycode.bang.anbang.utils.ErrorMessage;
import com.ycode.bang.anbang.utils.UrlUtils;

import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

/**
 * Created by ken on 2016/3/24.
 */
public class LoginActivity extends BaseActivity {
    EditText mEdtName, mEdtPassword;
    TextView mBtnLogin, mBtnFindBack, mBtnRegist;
    public static final String KEY_TOKEN = "key_token";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager
                .LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        addOurView(R.layout.activity_login);
        init();
    }

    private void init() {
        findView();
    }

    private void findView() {
        titleTextView.setText("登陆");

        mEdtName = (EditText) findViewById(R.id.edt_name);
        mEdtPassword = (EditText) findViewById(R.id.edt_pass);
        mBtnLogin = (TextView) findViewById(R.id.btn_login);
        mBtnFindBack = (TextView) findViewById(R.id.btn_findback);
        mBtnRegist = (TextView) findViewById(R.id.btn_regist);
        mBtnRegist.getBackground().setAlpha(100);//0~255透明度值

        mBtnLogin.setOnClickListener(myOnclick);
        mBtnFindBack.setOnClickListener(myOnclick);
        mBtnRegist.setOnClickListener(myOnclick);
    }

    View.OnClickListener myOnclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_login:

                    jumpIntoOtherUI( RepairListActivity.class);

                   /* if (TextUtils.isEmpty(mEdtName.getText().toString().trim())) {
                        toast("手机号不能为空！");
                    } else if (!CommonTools.isMobileNum(mEdtName.getText().toString().trim())) {
                        mEdtName.setText("");
                        toast("手机号格式不正确，请重新输入！");
                    } else if (TextUtils.isEmpty(mEdtPassword.getText().toString().trim())) {
                        mEdtPassword.setText("");
                        toast("请输入密码！");
                    } else {
                        progressDialog = ProgressDialog.show(baseContext, null, progressString,
                                true);
                        progressDialog.setCancelable(true);
                        String[] strings = {"username", mEdtName.getText().toString().trim(),
                                "password", mEdtPassword.getText().toString().trim(),
                                "deviceType", "android"};
                        getHttpPostRequest(UrlUtils.getLogin(), RegisiterModel.class, 2, strings);
                    }*/
                    break;
                case R.id.btn_findback:
                    break;
                case R.id.btn_regist:
                    break;
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {
        RegisiterModel regisiterModel = new RegisiterModel();
        regisiterModel = (RegisiterModel) response;
        if ("0".equals(regisiterModel.getCode())) {
            toast("登录成功");
            MyApp.instance.setToken(regisiterModel.getToken());
                jumpIntoOtherUI( RepairListActivity.class);
            JPushInterface.setAlias(baseContext, MyApp.instance.getToken(), new TagAliasCallback() {

                @Override
                public void gotResult(int i, String s, Set<String> set) {

                }
            });
            finish();
        } else if (!TextUtils.isEmpty(regisiterModel.getMessage())) {
            toast(regisiterModel.getMessage());
        } else {
            toast(ErrorMessage.getErrorMessage(regisiterModel.getCode()));
        }
        System.out.println("----" + regisiterModel.getToken());
    }


    /**
     * 退出应用
     */
    private long firstime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            long secondtime = System.currentTimeMillis();
            if (secondtime - firstime > 3000) {
                Toast.makeText(baseContext, "再按一次返回键退出", Toast.LENGTH_SHORT)
                        .show();
                firstime = System.currentTimeMillis();
                return true;
            } else {
                MyApp.instance.exit();
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
